package zuoban.controller;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import zuoban.memory.MyCustomMemory;
@CrossOrigin
@RestController
public class ChatMemoryController {


    private final ChatClient chatClient;

    public ChatMemoryController(ChatClient.Builder builder) {
        // 初始化一个自定义的内容记忆
        MessageChatMemoryAdvisor advisor = MessageChatMemoryAdvisor.builder(new MyCustomMemory()).conversationId("zuoban").build();
        this.chatClient = builder.defaultAdvisors(advisor).build();
    }


    @GetMapping("/chat-memory")
    public Flux<String> chatMemory(@RequestParam(defaultValue = "hello", required = false, name = "message") String message) {
        return chatClient.prompt("你是一位资深的java专家").user( message).stream().content();
    }
}
